package com.md.mainpage.presenter

import android.os.Parcel
import android.os.Parcelable
import com.md.basedpc.base.BasePresenter
import com.md.mainpage.interfaces.CategoryContract
import com.md.mainpage.interfaces.MainCategoriesContract
import com.md.mainpage.model.MainCategoryModel
import com.md.network.api.Category

class MainCategoryPresenter : BasePresenter<MainCategoriesContract.View>(), MainCategoriesContract.Presenter {

    private val model: MainCategoryModel by lazy {
        MainCategoryModel()
    }

    override fun getCategoriesList() {
        checkViewAttached()
        val disposable = model.getCategoryList()
                .subscribe({ categories ->
                    mRootView?.apply {
                        showCategoriesList(categories)
                    }

                }, { throwable ->
                    mRootView?.apply {
                        showError(throwable.toString())
                    }
                })

        disposable?.let { addSubscription(it) }
    }

    override fun getCategoryInfo(categoryId: Int) {
        checkViewAttached()
        val disposable = model.getCategoryDetailData(categoryId)
                .subscribe({ category ->
                    mRootView?.apply {
                        showSingleCategory(category)
                    }
                }, { throwable ->
                    mRootView?.apply {
                        showError(throwable.toString())
                    }
                })
        disposable?.let { addSubscription(it) }
    }


}